package com.example.imageopener;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private static final String LOG_TAG = MainActivity.class.getName();
	
    public static final String IMAGE_TYPE_UNSPECIFIED = "image/*";
	
	public static final int OPEN_IMAGE_MENU_ITEM = 0;
	
	public static final int OPEN_IMAGE_ACTIVITY_REQUEST = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        menu.add(Menu.NONE, OPEN_IMAGE_MENU_ITEM, Menu.NONE, R.string.open_image_menu_item);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case OPEN_IMAGE_MENU_ITEM: {
                Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
                
                innerIntent.setType(IMAGE_TYPE_UNSPECIFIED);

                Intent wrapperIntent = Intent.createChooser(innerIntent, getString(R.string.choose_image));
                startActivityForResult(wrapperIntent, OPEN_IMAGE_ACTIVITY_REQUEST);
            }
        }
        
        return true;
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (requestCode == OPEN_IMAGE_ACTIVITY_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
		    	Uri imageUri = data.getData();
		    	ImageView imageView = (ImageView) findViewById(R.id.image_content);
		    	imageView.setImageURI(imageUri);
            }
        }
	}
}
